package com.eelpo.framework.socket.handler;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelHandler.Sharable;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.WriteCompletionEvent;

import com.eelpo.framework.socket.SocketRequest;
import com.eelpo.framework.socket.listener.SocketContextListener;
import com.eelpo.framework.socket.listener.SocketRequestListener;
import com.eelpo.framework.socket.listener.SocketResponseListener;
import com.eelpo.framework.socket.session.SocketSession;

/**
 * 业务逻辑处理器
 * 
 * @author <a href="mailto:whz-work@163.com">汪洪州</a>
 * 
 * @since 1.0 Create on May 31, 2012
 */
@Sharable
public class SocketProcessHandler extends SimpleChannelUpstreamHandler {

	private SocketContextListener socketContextListener;
	private SocketRequestListener socketRequestListener;
	private SocketResponseListener socketResponseListener;

	public SocketProcessHandler() {

	}

	public SocketProcessHandler(SocketContextListener socketContextListener, SocketRequestListener socketRequestListener, SocketResponseListener socketResponseListener) {
		this.socketContextListener = socketContextListener;
		this.socketRequestListener = socketRequestListener;
		this.socketResponseListener = socketResponseListener;
	}

	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
		SocketSession socketSession = (SocketSession) ctx.getAttachment();
		try {
			SocketRequest.get().dispatch(socketSession, (ChannelBuffer) e.getMessage(), socketRequestListener);
		} catch (Exception ex) {
			if (socketRequestListener != null) {
				socketRequestListener.errorOnReceived(socketSession, ex);
			}
		}
	}

	@Override
	public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception {
		if (socketResponseListener != null) {
			socketResponseListener.writeComplete((SocketSession) ctx.getAttachment(), e);
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
		if (socketContextListener != null) {
			socketContextListener.exceptionCaught((SocketSession) ctx.getAttachment());
		}
	}

	@Override
	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		if (socketContextListener != null) {
			socketContextListener.channelConnected(ctx);
		}
		ctx.setAttachment(new SocketSession(ctx.getChannel()));
	}

	@Override
	public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		if (socketContextListener != null) {
			socketContextListener.channelClosed((SocketSession) ctx.getAttachment());
		}
	}

	public void setSocketContextListener(SocketContextListener socketContextListener) {
		this.socketContextListener = socketContextListener;
	}

	public void setSocketRequestListener(SocketRequestListener socketRequestListener) {
		this.socketRequestListener = socketRequestListener;
	}

	public void setSocketResponseListener(SocketResponseListener socketResponseListener) {
		this.socketResponseListener = socketResponseListener;
	}

}
